package com.lanju.dao;

import java.util.List;
import java.util.Map;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import org.springframework.stereotype.Repository;
import org.springframework.util.StringUtils;

import com.lanju.entity.ServiceComment;

@Repository
public class ServiceCommentDao {
	
    @PersistenceContext(unitName = "financialmarketingUnit")
    private EntityManager entityManager;
    
    public void insertServiceComment(ServiceComment serviceComment){
    	try {
    		entityManager.persist(serviceComment);
		} catch (Exception e) {
			e.printStackTrace();
		}
    }
    
    public List<ServiceComment> getCommentList(Map<String,Object> map){
    	try {
			int offset = 0;
			int limit = 0;
			if (!StringUtils.isEmpty(map.get("offset"))
					&& !StringUtils.isEmpty(map.get("limit"))) {
				offset = Integer.parseInt(map.get("offset").toString());
				limit = Integer.parseInt(map.get("limit").toString());
			}
    		StringBuffer sql = new StringBuffer();
    		sql.append(" from ServiceComment where serviceId='"+map.get("serviceId").toString()+"' and status=1 order by createTime desc");
    		Query query=entityManager.createQuery(sql.toString());
    		query.setFirstResult(offset).setMaxResults(limit);
    		return query.getResultList();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
    }
    public long getCommentListCount(Map<String,Object> map){
    	try {
    		StringBuffer sql = new StringBuffer();
    		sql.append(" from ServiceComment where serviceId='"+map.get("serviceId").toString()+"'  and status=1");
    		Query query=entityManager.createQuery(sql.toString());
    		return query.getResultList().size();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return 0;
    }
}
